<?php
/**
 * Parses the Mike-Hash that contains all data for a user. Returns the extracted user ID.
 * @return int The user ID 
 */
function RetrieveUserIDFromHash()
{
    if (!isset($_GET['hash'])) 
        die("10"); // niet ingelogd

    $split = explode(",", $_GET['hash']);

    // nakijken wat voor data in de hash zat
    if (count($split) != 2 || !is_numeric($split[0]) || strlen($split[1]) != 31)
        die("11"); // incorrecte hash

    // extract user id from hash
    $user_id = $split[0];

    $sql = "SELECT user_name, user_first_name, user_last_name, user_email FROM users WHERE user_id = '" . $user_id . "';";
    $result = mysql_query($sql);

    if (!$result) 
        die("2"); // fout in de SQL

    // user was niet gevonden
    if (mysql_num_rows($result) != 1) 
        die("12"); // gebruiker niet gevonden

    $row = mysql_fetch_array($result);

    if (!($split[1] == substr(md5($row['user_name'] . "b" . $row['user_first_name'] . "c" . $row['user_last_name'] . "d" . $row['user_email']), 1)))
        die("13"); // Do no attempt to forge the hash!
    
    return (int) $user_id;
}
?>